<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$criteria = new CDbCriteria();
		$criteria->order = 'voteStart ASC';

		/** @var Contest $contest */
		$contest = Contest::model()->find($criteria);

		if ($contest !== null)
		{
			$entries = new CActiveDataProvider('Entry', array(
				'criteria'=>array(
					'condition'=>'contestId=:contestId AND status=:status AND deleted=0', // we need the deleted here because beforeFind does not work here',
					'params'=>array(
						':contestId'=>$contest->id,
						':status'=>Entry::STATUS_APPROVED,
					),
					'order'=>'RAND()',
					'limit'=>32,
				),
				'pagination'=>false,
			));
		}
		else
			$entries = null;

		$this->render('index', array(
			'contest'=>$contest,
			'entries'=>$entries,
		));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model = new ContactForm;
		
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				/** @var PHPMailer $mailer */
				$mailer = Yii::app()->mailer;
				$mailer->CharSet = Yii::app()->charset;
				$mailer->From = Yii::app()->params['systemEmail'];
				$mailer->FromName = 'Kuvakilpailut.net';
				$mailer->AddAddress(Yii::app()->params['contactEmail']);
				$mailer->AddReplyTo($model->email);
				$mailer->Subject = Yii::t('email','Contact form').': '.$model->subject;
				$mailer->Body = $model->body;
				$mailer->Send();

				// The mailer extension does not allow to throw exceptions :(

				Yii::app()->user->setFlash(WebUser::FLASH_INFO,Yii::t('flash','Thank you for contacting us. We will respond to you as soon as possible.'));
				$this->redirect('index');
			}
		}
		
		$this->render('contact',array('model'=>$model));
	}
	
	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$loginModel = new LoginForm();
		$registerModel = new RegisterForm();

		// collect user input data
		if (isset($_POST['LoginForm']))
		{
			$loginModel->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($loginModel->validate() && $loginModel->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		
		// display the login form
		$this->render('login',array(
			'loginModel'=>$loginModel,
			'registerModel'=>$registerModel,
		));
	}

	/**
	 * Logs out the current user and redirect to the homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}